/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kamus.manager;

import com.kamus.DB;
import com.kamus.model.User;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UserManager extends BaseMgr{

    private static KosakataGaulMgr instance;

    public UserManager() {
        connection = getConnection();
    }

    public static KosakataGaulMgr getInstance() {
        if (instance == null) {
            instance = new KosakataGaulMgr();
        }
        return instance;
    }

    public boolean cekUser(User user) {
        boolean valid = false;
        try {
            psmnt = connection.prepareStatement("select PASSWORD from USER where USERNAME=?");
            psmnt.setString(1, user.getUsername());
            rs = psmnt.executeQuery();
            while (rs.next()) {
                if (user.getPassword().equals(rs.getString("password"))) {
                    valid = true;
                }
            }
        } catch (Exception e) {
        }
        return valid;
    }

    public int insertUser(User user) {
        int id = -1;
        cekConnection();
        try {
            psmnt = connection.prepareStatement("insert into USER(USERNAME,PASSWORD) values(?,?)");
            psmnt.setString(1, user.getUsername());
            psmnt.setString(2, user.getPassword());
            psmnt.executeUpdate();
            rs = psmnt.getGeneratedKeys();
            id = rs.getInt("id");
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

    public void deleteUser(int id) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("delete from USER where ID=?");
            psmnt.setInt(1, id);
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<User> getUser() {
        cekConnection();
        ArrayList<User> list = new ArrayList<User>();
        User user;
        try {
            psmnt = connection.prepareStatement("select * from USER order by username");
            rs = psmnt.executeQuery();
            while (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                list.add(user);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public void updateUser(User user) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("update USER set USERNAME=?, PASSWORD=? where ID= ?");
            psmnt.setString(1, user.getUsername());
            psmnt.setString(2, user.getPassword());
            psmnt.setInt(3, user.getId());
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
